package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtilsEnhancedTwo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Reference
    private FoodService foodService;

    //1. 文件下载
    @GetMapping("/templateDownload")
    public void templateDownload(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String templateRealPath = req.getSession().getServletContext().getRealPath("template/膳食库数据导入模板.xls");
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(templateRealPath));
            //把中文文件名进行转义,
            String fileName = URLEncoder.encode("膳食库数据导入模板.xls", "UTF-8");
            resp.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            ServletOutputStream servletOutputStream = resp.getOutputStream();
            int length;
            byte[] bytes = new byte[1024 * 8];
            while ((length = (bis.read(bytes))) != -1) {
                servletOutputStream.write(bytes, 0, length);
            }
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("下载失败");
        }
    }



    // 接收Excel文件   解析内容,把数据存入数据库  POST   /food/upload.do
    @PostMapping("/upload")
    public Result batchAdd(MultipartFile excelFile) {
        try {
            List<Food> foodList = new POIUtilsEnhancedTwo<Food>().getOrderSettingListFromExcelFile(excelFile, Food.class);
            for (Food food : foodList) {
                foodService.add(food);
            }
            return new Result(true, MessageConstant.IMPORT_FOOD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_FOOD_FAIL);
        }
    }


    //分页查询
    @RequestMapping("/findFoodsByPage.do")
    public PageResult findFoodsByPage(@RequestBody QueryPageBean queryPageBean) {
        return foodService.pageQuery(queryPageBean);
    }

    //新增
    @RequestMapping("/add.do")
    public Result add(@RequestBody Food food) {
        Result result = null;
        try {
            foodService.add(food);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加/修改食物项失败");
        }

        return new Result(true, "添加/修改食物项成功");
    }

    //根据id删除食物
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            foodService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除食物项失败");
        }
        return new Result(true, "删除食物项成功");
    }
}
